package net.dodoman.test;

import java.util.HashMap;
import java.util.Map;

import ognl.Ognl;

import org.junit.Assert;
import org.junit.Test;

/**
 * @author Downpour
 */
class User {

	private Integer id;

	private String name;

	private Department department = new Department();

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

}

/**
 * @author Downpour
 */
class Department {

	private Integer id;

	private String name;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

// setter and getters

class OGNLTestCase {

	@Test
	public void testGetValue() throws Exception {

		// Create root object
		User user = new User();
		user.setId(1);
		user.setName("downpour");

		// Create context
		Map context = new HashMap();
		context.put("introduction", "My name is ");

		// Test to directly get value from root object, with no context
		Object name = Ognl.getValue(Ognl.parseExpression("name"), user);
		Assert.assertEquals("downpour", name);

		// Test to get value(parameter) from context
		Object contextValue = Ognl.getValue(Ognl
				.parseExpression("#introduction"), context, user);
		Assert.assertEquals("My name is ", contextValue);

		// Test to get value and parameter from root object and context
		Object hello = Ognl.getValue(Ognl
				.parseExpression("#introduction + name"), context, user);
		Assert.assertEquals("My name is downpour", hello);

	}

	/**
	 * 
	 * @throws Exception
	 */
	@Test
	public void testSetValue() throws Exception {
		// Create root object
		User user = new User();
		user.setId(1);
		user.setName("downpour");
		// Set value according to the expression
		Ognl.setValue("department.name", user, "dev");
		Assert.assertEquals("dev", user.getDepartment().getName());

	}

}